
//
//  XWNewsViewController.m
//  CGWSWW
//
//  Created by lanou3g on 16/8/15.
//  Copyright © 2016年 CXG. All rights reserved.
//


#import "XWNewsViewController.h"
#import "XWTopViewController.h"
#import "XWOtherTableViewController.h"
#import "HeadHelp.h"

#define MS(MyWeakSelf)  __weak __typeof(&*self)MyWeakSelf = self;
#define kLabel_width KScreen_Width/4.5

@interface XWNewsViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic)  UIScrollView *titleScrollView;
@property (strong, nonatomic)  UIScrollView *contentScrollView;
@property(nonatomic,strong)NSMutableArray *titleLabels;

@property(nonatomic,weak)UILabel *selLabel;
@end
static CGFloat radio = 1.3;
@implementation XWNewsViewController

-(NSMutableArray*)titleLabels{
    if (!_titleLabels) {
        _titleLabels = [NSMutableArray array];
    }
    return _titleLabels;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"newTitle"]];
    //关闭第一个scrollView自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    //添加所有子控制器
    [self setupChildViewController];
   
    //设置滚动视图
    [self setUpScrollView];
    //添加所有子控制器的对应标题
    [self setupTitleLabel];
    
}
#pragma mark 添加所有子控制器
-(void)setupChildViewController{

    
    NSArray *namearray = [NSArray array];
    namearray = @[@"国内",@"国际",@"娱乐",@"体育",@"科技",@"奇闻趣事",@"生活健康"];
    NSArray *contentarray = [NSArray array];
    contentarray = @[@"guonei",@"world",@"huabian",@"tiyu",@"keji",@"qiwen",@"health"];
    
    XWTopViewController *oneViewController = [[XWTopViewController alloc] init];
    oneViewController.title = @"社会";
    [self addChildViewController:oneViewController];
    
    for(int i = 0; i < namearray.count; i++)
    {
        XWOtherTableViewController *otherViewController = [[XWOtherTableViewController alloc] init];
        otherViewController.title = namearray[i];
        otherViewController.content = contentarray[i];
        [self addChildViewController:otherViewController];
    }
    
}

-(void)setUpScrollView{
   
    _contentScrollView = [UIScrollView new];
    [self.view addSubview:_contentScrollView];
    _titleScrollView =[UIScrollView new];
    [self.view addSubview:_titleScrollView];
    self.contentScrollView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
     self.titleScrollView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.titleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    NSInteger count = self.childViewControllers.count;
    self.titleScrollView.contentSize = CGSizeMake(count*kLabel_width, 0);
    self.titleScrollView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.95];
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    //
    self.contentScrollView.contentSize =CGSizeMake(KScreen_Width*count, 0);
    self.contentScrollView.delegate = self;
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsVerticalScrollIndicator = NO;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.bounces = NO;
    self.contentScrollView.backgroundColor = XWCommonColor;
    self.titleScrollView.translatesAutoresizingMaskIntoConstraints = NO;
    self.contentScrollView.translatesAutoresizingMaskIntoConstraints = NO;
    self.contentScrollView.scrollsToTop = NO;
    self.titleScrollView.scrollsToTop = NO;

}
#pragma mark 添加所有子控制器的对应标题
-(void)setupTitleLabel{
    NSInteger count = self.childViewControllers.count;

    CGFloat label_y = 0;
    CGFloat label_width =kLabel_width;
    CGFloat label_high = 44;
    for (int i= 0; i<count; i++) {
        //获取子控制器
        UIViewController *childVc = self.childViewControllers[i];
        UILabel *label = [[UILabel alloc]init];
        //label 自动交互是关闭的
        label.userInteractionEnabled = YES;
        label.frame = CGRectMake(i*label_width, label_y, label_width, label_high);
        label.text = childVc.title;

        label.textAlignment = NSTextAlignmentCenter;
        [self.titleLabels addObject:label];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleClick:)];
        [label addGestureRecognizer:tap];
        if (i == 0) {
            [self titleClick:tap];

        }
        //给label一个tag值 方便获取
        label.tag  = i ;
        [self.titleScrollView addSubview:label];
        
    }
    
}
#pragma mark 头部标题点击
-(void)titleClick:(UITapGestureRecognizer*)sender{
    //点击label高亮状态
    UILabel *selLabel = (UILabel*)sender.view;
    [self selectLabel:selLabel];
    //滚动对应的位置
    NSInteger index = selLabel.tag;
    //计算滚动的位置
    CGFloat offsetX =index * KScreen_Width;
    self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
    //添加子控制器
    [self showChildVc:index];
    //让labele居中
    [self setUpTitleCenter:selLabel];
    
}
#pragma mark 设置标题选中居中
-(void)setUpTitleCenter:(UILabel*)centerLabel{
    //计算偏移量
    CGFloat offsetX = centerLabel.center.x - KScreen_Width*0.5;
    //标题滚动条
    if (offsetX < 0) offsetX = 0;
    //最大偏移量
    CGFloat maxOffsetX = self.titleScrollView.contentSize.width - KScreen_Width;
    if (offsetX >maxOffsetX) {
        offsetX = maxOffsetX;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
-(void)selectLabel:(UILabel*)label{
     _selLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
    //取消设置
    _selLabel.transform =CGAffineTransformIdentity;;
    //高亮
     label.textColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
    //改变形变
    label.transform = CGAffineTransformMakeScale(radio, radio);
    _selLabel = label;
}
#pragma mark 滚动时调用
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat curPage = scrollView.contentOffset.x/scrollView.bounds.size.width;
    //左边
    NSInteger leftPage = curPage;
    //右边
    NSInteger rightPage = curPage+1;
    UILabel *leftlabel = self.titleLabels[leftPage];
    UILabel *rightLabel ;
    if (rightPage < self.titleLabels.count-1) {
        rightLabel = self.titleLabels[rightPage];
        
    }
    CGFloat rightScale = curPage -leftPage;
    CGFloat leftScale = 1 -rightScale ;
    
    //缩放放大
    //1-1.3
    rightLabel.textColor = [UIColor colorWithRed:rightScale green:0 blue:0 alpha:1];
    leftlabel.textColor = [UIColor colorWithRed:leftScale green:0 blue:0 alpha:1];
    rightLabel.transform = CGAffineTransformMakeScale(rightScale * 0.3+1, rightScale * 0.3+1);
    leftlabel.transform = CGAffineTransformMakeScale(leftScale * 0.3+ 1, leftScale * 0.3 +1);
    
}
#pragma mark 滚动减速停止调用
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    XWLogFunc
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    //把对应的标题选中
    UILabel *label = _titleLabels[index];
    [self selectLabel:label];
    //添加子控制器view
    [self showChildVc:index];
    //让labele居中
    [self setUpTitleCenter:label];
    
}
#pragma mark 显示子控制器
-(void)showChildVc:(NSInteger)index{
    CGFloat offsetX =index * KScreen_Width;
    
    UIViewController *childVc = self.childViewControllers[index];
    //如果加载直接返回
    if (childVc.isViewLoaded)return;
    
    childVc.view.frame = CGRectMake(offsetX, 0, self.contentScrollView.bounds.size.width, self.contentScrollView.bounds.size.height);
    [self.contentScrollView addSubview:childVc.view];
    
}
@end